package com.tomcov.client.activities;

import java.util.Date;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.tomcov.client.ClientFactory;
import com.tomcov.client.service.MessageServiceAsync;
import com.tomcov.client.service.UserServiceAsync;
import com.tomcov.client.view.NewMessageView;
import com.tomcov.shared.Message;
import com.tomcov.shared.UserInfo;

public class NewMessageActivity extends AbstractActivity implements NewMessageView.Presenter{

	private ClientFactory clientFactory;


	/**
	 * Instantiates a new new message activity.
	 *
	 * @param clientFactory the client factory
	 */
	public NewMessageActivity(ClientFactory clientFactory){
		this.clientFactory = clientFactory;	
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		NewMessageView NewMessageView = clientFactory.getNewMessageView();
		NewMessageView.getbutton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				NewMessageView NewMessageView = clientFactory.getNewMessageView();
				NewMessageView.getbutton().setEnabled(false);
				NewMessageView.getbutton().setVisible(false);
				sendmessage();
				NewMessageView.getbutton().setVisible(true);
				NewMessageView.getbutton().setEnabled(true);
			}
		});

		NewMessageView.setPresenter(this);
		panel.setWidget(NewMessageView.asWidget());


	}

	@Override
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);		
	}


	private void sendmessage() {
		UserServiceAsync USA= clientFactory.getUSA();
		USA.getUserInSession(new AsyncCallback<UserInfo>(){
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("fail to get in session");
			}
			@Override
			public void onSuccess(UserInfo result) {
				if(result!=null){
					final String name=result.getUserName();
					NewMessageView NewMessageView = clientFactory.getNewMessageView();
					UserServiceAsync USA= clientFactory.getUSA();
					USA.getUserByName(NewMessageView.To(),new AsyncCallback<UserInfo>(){

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("this user doesnt exist");
						}

						@Override
						public void onSuccess(UserInfo resultat) {

							if (resultat!=null){
								NewMessageView NewMessageView = clientFactory.getNewMessageView();
								MessageServiceAsync MSA=clientFactory.getMSA();
								Date today=new Date();
								Message message = new Message(name,NewMessageView.To(),NewMessageView.getmessagetosend(),today.toString());
								MSA.AddMessage(message,new AsyncCallback<Message>(){
									@Override
									public void onFailure(Throwable caught) {
										Window.alert("fail to send message");

									}
									@Override
									public void onSuccess(Message result) {
										Window.alert("message sent");
										//goTo(new MapPlace(null));

									}
								});
							}
							else{
								Window.alert("user doesn't exist");
							}
						}
					});
				}
				else{
					Window.alert("get null session");
				}
			}
		});
	}

	
}

